﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Itop.YHPC.Model;
using Itop.YHPC.controller;
using System.Text;

namespace Itop.YHPC.admin {
    public partial class sysprogTreeAjax : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            
            string id = Request.QueryString["node"];
            object data=null;
            string action=Request.QueryString["action"];
            if (action == "proggrid")
                data = getListGroupProgGrid(id);
            else if (action == "groupprog")
                data = getListGroupProg(id);
            else if (action == "userid")
                data = getListGroupUserGrid();
            else
                data = getList(id);
            string ret = Newtonsoft.Json.JsonConvert.SerializeObject(data);
            //if (ret == "[]")
            //{
            //    //读取上传的数据流
            //    System.IO.StreamReader sr = new System.IO.StreamReader(Request.InputStream, Encoding.UTF8);
            //    string json = sr.ReadToEnd();
            //    json = util.U2G(json);//字符串中的中文unicode字符转成中文
            //    IList<retModel> list = null;//
            //    try
            //    {
            //        list = new List<retModel>();//
            //        retModel ob = Newtonsoft.Json.JsonConvert.DeserializeObject<retModel>(json);
            //        list.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<retModel>(json));
            //    }
            //    catch (Exception ex)
            //    {//可能是对象数组

            //    }
            //    foreach (retModel sg in list)
            //    {
            //        if (sg.GroupID == "")
            //        {
            //            string aa = "";
            //        }
            //    }
            //    ret = Newtonsoft.Json.JsonConvert.SerializeObject(json);
            //    Response.Write(ret);
            //}
            //else
            Response.Write(ret);
            Response.End();
        }
        private List<object> getListGroupUserGrid() {
            List<object> list = new List<object>();
            string userid = Request.QueryString["userid"];
            string where = "where ";
            if (!string.IsNullOrEmpty(userid))
                where += "  id not in (select groupid from sysgroupuser where userid='" + userid + "')";
            IList<sysgroup> orgs = sysgroupController.GetList(where);
            foreach (sysgroup org in orgs) {
                list.Add(new { GroupID = org.id, GroupName = org.GroupName, UserID = userid });
            }
            return list;
        }
        private List<object> getListGroupProgGrid(string pid) {
            List<object> list = new List<object>();
            string groupid = Request.QueryString["groupid"];
            string where = "where isgroup='0' ";
            if (!string.IsNullOrEmpty(groupid))
                where += " and id not in (select progid from sysgroupprog where groupid='" + groupid + "')";
            IList<sysprog> orgs = sysprogController.GetList(where);
            foreach (sysprog org in orgs) {
                list.Add(new { ProgID = org.id, ProgName = org.ProgName, GroupID = groupid });
            }
            return list;
        }
        private List<object> getListGroupProg(string pid) {
            List<object> list = new List<object>();

            IList<sysprog> orgs = sysprogController.GetList("where parentid='" + pid + "'");
            foreach (sysprog org in orgs) {
                if (org.IsGroup == "1") {
                    list.Add(new { id = org.id, cls = "folder", text = org.ProgName });
                } else {
                    list.Add(new { id = org.id, cls = "file", leaf = true,  text = org.ProgName });
                }
            }
            return list;
        }
        
        List<object> getList(string pid) {

            List<object> list = new List<object>();
            
            IList<sysprog> orgs=sysprogController.GetList("where parentid='" + pid + "'");
            foreach (sysprog org in orgs) {
                if (org.IsGroup == "1") {
                    list.Add(new { id = org.id, cls = "folder",  text = org.ProgName });
                } else {
                    list.Add(new { id = org.id, cls = "file", leaf = true, text = org.ProgName });
                }
            }
            return list;
        }
        //class treenode {
        //    public string id;
        //    public string text;
        //    public bool leaf;
        //    public bool Checked;
        //    public string cls;
        //}

        class retModel
        {
           
           public string GroupID;
           public string GroupName;
           public string UserID;
           // public string message = "Created record";
        }
    }
}
